package com.swh.service.Impl;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swh.dao.SysAreaDao;
import com.swh.entity.QualificationCondition;
import com.swh.entity.SysArea;
import com.swh.entity.SysAreaListener;
import com.swh.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/12/14 14:46
 * @Version: 1.0
 */
@Service
public class SysAreaServiceImpl extends BaseServiceImpl<SysArea> implements SysAreaService {

    @Autowired
    public SysAreaServiceImpl(SysAreaDao sysAreaDao) {
        super(sysAreaDao);
    }

    @Autowired
    private SysAreaDao sysAreaDao;


    @Override
    public PageInfo<Map> selectPage(Integer pageNum, Integer pageSize, QualificationCondition condition) {
        PageHelper.startPage(pageNum,pageSize);
        List<Map> list = sysAreaDao.selectPage((pageNum - 1) * pageSize, pageSize, condition);
        return new PageInfo<>(list);
    }

    @Override
    public List<SysArea> findAll(SysArea sysArea) {
        return sysAreaDao.findAll(sysArea);
    }

    @Override
    public List<SysArea> findAll2() {
        return sysAreaDao.findAll2();
    }

    //文件上传
    @Override
    public void upload(InputStream is) {
        EasyExcel.read(is,SysArea.class,new SysAreaListener<SysArea>(sysAreaDao)).sheet().doRead();
    }


    //文件下载
    @Override
    public void download(OutputStream os) {
        SysArea sysArea = new SysArea();
        sysArea.setDelFlag("0");
        EasyExcel.write(os, SysArea.class)
                .sheet("区域管理").doWrite(sysAreaDao.findAll2());
    }

    @Override
    public List<SysArea> findAllChild(Integer id) {
        return sysAreaDao.findAllChild(id);
    }

    @Override
    public int deleteChild(Integer id) {
        List<SysArea> allChild = sysAreaDao.findAllChild(id);
        ArrayList<Long> list = new ArrayList<>();
        for (SysArea sysArea : allChild) {
            list.add(sysArea.getId());
        }
        return sysAreaDao.deleteChild(list);
    }
}
